#include "Sprite.h"
#include "Macros.h"
#include "CDXManager.h"

namespace Framework
{
	CSprite::CSprite(char* path)
	{
		dxSprite = CDXManager::GetInstance()->GetDXSprite();
		D3DXIMAGE_INFO imageInfo;
		D3DXGetImageInfoFromFile(path,&imageInfo);
		width=imageInfo.Width;
		height=imageInfo.Height;
		D3DXCreateTextureFromFileEx(CDXManager::GetInstance()->GetDevice(),
			path,
			imageInfo.Width,
			imageInfo.Height,
			1,						//Mipmap level
			D3DPOOL_DEFAULT,
			D3DFMT_UNKNOWN,
			D3DPOOL_DEFAULT,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(255,0,255),
			&imageInfo,
			NULL,
			&image);
	}
	void CSprite::Draw(int x,int y)
	{
		D3DXMATRIX* mTransform = new D3DXMATRIX();
		D3DXMatrixIdentity(mTransform);

		dxSprite->SetTransform(mTransform);
		dxSprite->Draw(image,NULL,NULL,&D3DXVECTOR3(0.0f, 0.0f, 0.0f),D3DCOLOR_XRGB(255,255,255));

		delete mTransform;
		mTransform = 0;
	}

	void CSprite::Draw(int x,int y, float xScaleRatio, float yScaleRatio,D3DXVECTOR2 vRotatteCenter,float angle,D3DCOLOR color,float deep,Animation *anim)
	{
		D3DXMATRIX* mTransform = new D3DXMATRIX();
		D3DXMatrixTransformation2D(mTransform,NULL,0,&D3DXVECTOR2(xScaleRatio,yScaleRatio),&vRotatteCenter,angle,&D3DXVECTOR2((float)x,(float)y));

		dxSprite->SetTransform(mTransform);
		dxSprite->Draw(image,&anim->GetRect(),NULL,&D3DXVECTOR3(0,0,deep),color);

		delete mTransform;
		mTransform = 0;
	}
	CSprite::~CSprite(void)
	{
		SAFE_DEL(image);

		if (dxSprite)
		{
			dxSprite->Release();
		}
	}

}